<?php

/**
 * @author liyi
 * @copyright 2018.12.19
 */
namespace Pwtwogame2\Controller;
use Common\Controller\ApiiController;
use Think\Model;
use Think\Log;

class LoginController extends ApiiController {

    const GAME_TYPE = 'Pwtwogame';  // 游戏类型

    const DB_USER_NAME = 'pwtwo_game2.users'; // 游戏数据表

    const TOURIST = "pwzwtwos2:tourist:sid:%"; // 游戏游客用户

    const AUTH_USER = "pwzwtwos2:auth_user:sid:%";  // 授权用户

    const CACHE_KEY = 'pwzwtwos2';  // 游戏全局缓存键

    const MINING_COUNT_KEY = "pwzwtwos2:count"; // 渠道来源统计缓存键

    const USER_SHARE = 'pwzwtwonew2:share:%s:type:%s';  // 点击分享按钮的次数

    const MINING_DBID = 8; // redis数据库

    // 登陆授权接口
    public function login()
    {
        // 记录开始时间
        $begin_memory = memory_get_usage();
        $starttime = microtime(true);
        $gameType = self::GAME_TYPE;
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');
        $id = I('id','','int');
        $openid = I('openid','','string');
        $sid = I('source',0,'int');

        if(empty($openid)){
            $this->response(2,'openid不能为空');
        }

        $data['openid'] = $openid;
        $data['nickname'] = $nickname;
        $data['avatar'] = $avatar;
        $data['id'] = $id;

        $dbname = self::DB_USER_NAME;
        $cacheKey = self::CACHE_KEY;
        $cacheval = sprintf(self::AUTH_USER,$sid);

        $dbId = self::MINING_DBID;// redis数据库

        $userInfo = $this->commLogin_p2($dbname,$data,$cacheKey,$cacheval,$dbId);
        // 分享奖励
        $userid = isset($userInfo['userid'])?$userInfo['userid']:'';
        if(!empty($userid)){
            $redis = REDIS('activity');
            $redis->select(8);

            $shareKey = sprintf(self::USER_SHARE,$userid,1);  //分享键
            $vedioKey = sprintf(self::USER_SHARE,$userid,3);  //视频键
            $goldKey = sprintf(self::USER_SHARE,$userid,4);  //钻石换金币次数

            $share = $redis->get($shareKey);
            $vedio = $redis->get($vedioKey);
            $gold = $redis->get($goldKey);

            $share_count = array(1=>intval($share), 3=>intval($vedio), 4=>intval($gold));
        }

        $userInfo['share_count'] = $share_count;
        $userInfo['serverTime'] = time();

        if($sid){   // redis 渠道统计
            $incr = $userInfo['incr'];
            $this->getChannelCount(self::MINING_COUNT_KEY,$sid,'auth_user',$incr,$dbId);
        }

        // 记录结束时间
        $endtime = microtime(true);
        // 输出运行时间
        $times = round($endtime-$starttime,3).'秒';
        log::write("授权登陆用时：".$times);
        $this->response(0,'ok',$userInfo);
    }

    //未授权登陆接口
    public function loginNotAuth() {
        // 记录开始时间
        $memory = memory_get_usage();
        $starttime = microtime(true);
        $code = I('code','','string');
        $sid = I('source',0,'int');

        if(!$code) {
            $this->response(1,'code不能为空');
        }
        $gameType = self::GAME_TYPE;    // 配置文件名称
        $dbname = self::DB_USER_NAME;   // 数据库users表
        $cacheKey = self::CACHE_KEY;    // 游戏缓存键
        $cacheval = sprintf(self::TOURIST,$sid);
        $dbId = self::MINING_DBID;      // redis数据库 记录游客
        $userInfo = $this->commLoginNotAuth_p2($gameType,$dbname,$code,$cacheKey,$cacheval,$dbId,$sid);
        // 分享奖励
        $userid = isset($userInfo['userid'])?$userInfo['userid']:'';
        if(!empty($userid)){
            $redis = REDIS('activity');
            $redis->select(8);

            $shareKey = sprintf(self::USER_SHARE,$userid,1);  //分享键
            $vedioKey = sprintf(self::USER_SHARE,$userid,3);  //视频键
            $goldKey = sprintf(self::USER_SHARE,$userid,4);  //钻石换金币次数

            $share = $redis->get($shareKey);
            $vedio = $redis->get($vedioKey);
            $gold = $redis->get($goldKey);

            $share_count = array(1=>intval($share), 3=>intval($vedio), 4=>intval($gold));
        }

        $userInfo['share_count'] = $share_count;
        $userInfo['serverTime'] = time();

        if($sid){   // redis 渠道统计
            $incr = $userInfo['incr'];
            $this->getChannelCount(self::MINING_COUNT_KEY,$sid,'tourist',$incr,$dbId);
        }

        // 记录结束时间
        $endtime = microtime(true);
        // 输出运行时间
        $times = round($endtime-$starttime,3).'秒';
        $memory2 = memory_get_usage();
        log::write("未授权登陆用时：".$times);
        log::write("未授权登陆内存消耗：".$this->convert($memory2-$memory));
        $this->response(0,'ok',$userInfo);
    }


    public function setCookis(){
        // 记录开始时间
        $starttime = microtime(true);
        log::write("开始时间：".$starttime);
        // 记录结束时间
        $endtime = microtime(true);
        log::write("结束时间：".$endtime);
        // 输出运行时间
        $times = round($endtime-$starttime,3).'秒';
        log::write("用时：".$times);
    }

    public function getRedis(){
        $gameType = self::GAME_TYPE;
        $openid = 'oyP8f5TUO_FvjNsrl-VjXtbIgkRw';
        $redis = REDIS('activity');
        $redis->select(8);
        $userTemp = $redis->hGet($gameType,$openid);
        print_r(unserialize($userTemp));
    }

    public function ceshi(){
        $url = "/home/wwwroot/static.bdwha.com/sjxj_game/game.json";
        $json_string = file_get_contents($url);
        $info = json_decode(trim($json_string,chr(239).chr(187).chr(191)),true);
        var_dump($info['num']);
    }


}